package com.woniuxy02;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 作者
 * @create 2025-09-08 14:24
 */
public class Test05 {
    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student(1,"zs",15);
        students[1]=new Student(2,"ls",17);
        students[2]=new Student(3,"ww",12);
        //new 接口实现类对象 ，先定义类，在类中实现了接口中的方法
        Comparator comparator1=new StudentNameAsc();
        //new 接口实现类对象没有定义类,没有定义类,代码简洁
        //缺点：不能重用
        Comparator comparator=new Comparator(){
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Student s1 && o2 instanceof Student s2){
                    return s1.getName().compareTo(s2.getName());
                }
                return 0;
            }
        };
        System.out.println("姓名升序");
        Arrays.sort(students,comparator);
        System.out.println(Arrays.toString(students));
        System.out.println("姓名降序");
        Arrays.sort(students,new Comparator(){
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Student s1 && o2 instanceof Student s2){
                    return s2.getName().compareTo(s1.getName());
                }
                return 0;
            }
        });
        System.out.println(Arrays.toString(students));
        System.out.println("年龄降序");
        Arrays.sort(students,new Comparator(){
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Student s1 && o2 instanceof Student s2){
                    return s2.getAge()-s1.getAge();
                }
                return 0;
            }
        });
        System.out.println(Arrays.toString(students));

    }



}

